package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderMapper.findById(id);

       /* if (map != null) {
            map.put("orderDate",DateUtils.parseDate2String((Date)map.get("orderDate")));
        }*/
        return map;
    }

    //预约功能实现
    @Override
    public Result order(Map map) throws Exception {
        //获取预约日期
       String orderDate =  (String)map.get("orderDate");
        //根据预约日期查询预约详情
        OrderSetting orderSetting = ordersettingMapper.findByOrderDate(DateUtils.parseString2Date(orderDate));
        //1. 如果预约详情为空则该预约日期未设置预约
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //获取可预约人数
        int number = orderSetting.getNumber();
        //获取已预约人数
        int reservations = orderSetting.getReservations();

        //2. 如果已预约人数 >= 可预约人数  则预约已满,无法预约
        if (reservations >= number) {
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        //3. 检查用户是否重复预约
        String telephone = (String)map.get("telephone");
        //查询该手机号的用户是否重复预约
        Member member = memberMapper.findByTelephone(telephone);
        if (member != null) {
            //获取用户会员id
            Integer memberId = member.getId();
            //预约日期
            Date date = DateUtils.parseString2Date(orderDate);
            //获取套餐id
            String setmealId = (String)map.get("setmealId");
            //封装预约信息
            Order order = new Order(memberId, date,Integer.parseInt(setmealId));
            //查询是否存在重复预约
            List<Order> orderList = orderMapper.findByCondition(order);
            if (orderList != null && orderList.size() > 0) {
                //存在重复预约,无法完成再次预约
                return  new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberMapper.add(member);//自动完成会员注册
        }
        //预约成功,新增预约订单信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType((String)map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        orderMapper.add(order);

        //设置已预约人数,更新当日已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        ordersettingMapper.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }
}
